




/*
*************************************************************************
*西瑞克斯研发管理系统
*版 本: 1.0   
*日 期：02/15/2012    
*作 者： 王浩 QQ：873987060   
*************************************************************************   
*/ 
package com.application.yfoutsourcecontarct.struts.actions;
import java.io.File;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.application.attachment.entity.Attachment;
import com.application.attachment.service.AttachmentService;
import com.application.attachment.struts.form.AttachmentForm;
import com.application.usertable.entity.Usertable;
import com.application.yfexamine.entity.YfExmaine;
import com.application.yfexamine.service.YfExmaineService;
import com.application.yfexamine.struts.form.YfExmaineForm;
import com.application.yfoutsourcecontarct.commons.Constants;
import com.application.yfoutsourcecontarct.entity.YfOutsourceContract;
import com.application.yfoutsourcecontarct.service.YfOutsourceContractService;
import com.application.yfoutsourcecontarct.struts.form.YfOutsourceContractForm;
import com.manager.commons.CommonUtil;
import com.manager.commons.DisplayTagPageBean;
import com.manager.commons.RandomKey;
import com.manager.commons.page.PageBean;
import com.manager.workflow.WorkFlowConstants;
import com.manager.workflow.core.WorkFlowCommon;
import com.manager.workflow.core.YfWorkFlowDefine;
 /**
 * 
 * Description:YfOutsourceContractAction<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 王浩 QQ：873987060    <br />
 * 
 * History: <br />
 * 
 * 02/15/2012 
 * 
 */
public class YfOutsourceContractAction extends DispatchAction
{
       
       /**
	 * Service层实例
	 */
	private YfOutsourceContractService yfOutsourceContractService;
	
	private WorkFlowCommon workFlowCommon;
	
	private YfExmaineService yfExmaineService;

	/**
	 * 日志
	 */
	private Logger log = Logger.getLogger(this.getClass());

	/**
	 * 实体对象
	 */
        private YfOutsourceContract yfOutsourceContract;
        
        private AttachmentService attachmentService;


        /**
	 * 采用partialList方式分页程序
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getYfOutsourceContractPartialList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("searchYfOutsourceContract" + "开始");
		YfOutsourceContractForm yfOutsourceContract = (YfOutsourceContractForm)form;
		// 统计总记录数的sql语句
		int resultSize = 0;
		// 初始化pageBean供分页所用
		PageBean pageBean = null;
		// 取得当前页list数据
		List yfOutsourceContractList = null;
		resultSize = yfOutsourceContractService.getAllCountYfOutsourceContract(yfOutsourceContract);
		pageBean = PageBean.getPageBean("yfOutsourceContractTab", resultSize,request);
		yfOutsourceContractList = yfOutsourceContractService.searchYfOutsourceContract(yfOutsourceContract,pageBean);
		request.setAttribute("yfOutsourceContractList", yfOutsourceContractList);
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		log.debug("searchYfOutsourceContract" + "结束");

		return mapping.findForward(Constants.SUCCESS);
	}

        /**
	 * 到达新增页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toAddYfOutsourceContract(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("toAddYfOutsourceContracts");
		YfOutsourceContractForm yfOutsourceContractForm = (YfOutsourceContractForm) form;
		yfOutsourceContractForm.setId(RandomKey.generateID_Long_19());
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 增加
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward addYfOutsourceContract(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("addYfOutsourceContracts");
		YfOutsourceContract yfOutsourceContract = this.getYfOutsourceContract(form);
		 yfOutsourceContract.setOperateid(CommonUtil.getCurrentUser(request).getId());
		 yfOutsourceContract.setCreateDate(new Date());
		 
		 String outNo = yfOutsourceContractService.getMaxOutNo(yfOutsourceContract.getProjectNo());
		 yfOutsourceContract.setOutsourceNo(outNo);
		 yfOutsourceContract.setAuditStatus(WorkFlowConstants.AUDIT_STATUS_1);
		this.yfOutsourceContractService.insertObject(yfOutsourceContract);
		return mapping.findForward(Constants.SUCCESS);
	}
        
	/**
	 * 删除
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward deleteYfOutsourceContract(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("deleteYfOutsourceContracts");
		YfOutsourceContractForm yfOutsourceContractForm = (YfOutsourceContractForm) form;
		this.yfOutsourceContractService.deleteYfOutsourceContracts(yfOutsourceContractForm);
		String serverPath=CommonUtil.ATTACHE_FILE;
		if(StringUtils.isBlank(serverPath)){
			serverPath=request.getSession().getServletContext().getRealPath("/"); 
		}
		AttachmentForm attachmentForm = new AttachmentForm();
		attachmentForm.setFid(yfOutsourceContractForm.getId().toString());
		attachmentForm.setType(com.manager.commons.Constants.FJ_TYPE_WXHT);
		List<Attachment> attachment = attachmentService.queryAttachment(attachmentForm);
		if(attachment!=null && attachment.size()>0){
			for (Attachment attachment2 : attachment) {
				File file = new File(serverPath + attachment2.getUrl());
		        file.delete();
				attachmentService.deleteObject(attachment2);
				
			}
		}
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 到达更新页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toUpdateYfOutsourceContract(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("toUpdateYfOutsourceContracts");
		YfOutsourceContractForm yfOutsourceContractForm = (YfOutsourceContractForm) form;
		yfOutsourceContract = this.yfOutsourceContractService.searchYfOutsourceContract(yfOutsourceContractForm, null).get(0);
		request.setAttribute(Constants.YFOUTSOURCECONTRACT, yfOutsourceContract);
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		return mapping.findForward(Constants.SUCCESS);
	}

        
	/**
	 * 更新
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward updateYfOutsourceContract(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("updateYfOutsourceContracts");
		YfOutsourceContract yfOutsourceContract = this.getYfOutsourceContract(form);
	        yfOutsourceContract.setOperateid(CommonUtil.getCurrentUser(request).getId());
		 yfOutsourceContract.setCreateDate(new Date());
		this.yfOutsourceContractService.updateObject(yfOutsourceContract);

		return mapping.findForward(Constants.SUCCESS);
	}
        
	/**
	 * 查看页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward viewYfOutsourceContract(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		log.debug("viewYfOutsourceContract");
		YfOutsourceContractForm yfOutsourceContractForm = (YfOutsourceContractForm) form;
		yfOutsourceContract = this.yfOutsourceContractService.searchYfOutsourceContract(yfOutsourceContractForm, null).get(0);
		request.setAttribute(Constants.YFOUTSOURCECONTRACT, yfOutsourceContract);
		String processId = yfOutsourceContract.getProcessId();
		if (StringUtils.isNotBlank(processId)) {
			examineList(yfOutsourceContract.getProcessId(),yfExmaineService,request);
		}
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		return mapping.findForward(Constants.SUCCESS);
	}

	// get--set
	/**
	 * @return the YfOutsourceContractService
	 */
	public YfOutsourceContractService getYfOutsourceContractService() {
		return this.yfOutsourceContractService;
	}

	/**
	 * @param YfOutsourceContractService
	 *  the YfOutsourceContractService to set
	 */
	public void setYfOutsourceContractService(YfOutsourceContractService yfOutsourceContractService) {
		this.yfOutsourceContractService = yfOutsourceContractService;
	}

	/**
	 * 获取YfOutsourceContract实体
	 * @param ActionForm
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @return the YfOutsourceContract
	 */
	public YfOutsourceContract getYfOutsourceContract(ActionForm form) throws IllegalAccessException, InvocationTargetException {
		YfOutsourceContractForm yfOutsourceContractForm = (YfOutsourceContractForm) form;
		yfOutsourceContract = new YfOutsourceContract();
		BeanUtils.copyProperties(yfOutsourceContract, yfOutsourceContractForm);
		return yfOutsourceContract;
	}
    
        public void setYfOutsourceContract(YfOutsourceContract yfOutsourceContract) {
		this.yfOutsourceContract = yfOutsourceContract;
	}

		public AttachmentService getAttachmentService() {
			return attachmentService;
		}

		public void setAttachmentService(AttachmentService attachmentService) {
			this.attachmentService = attachmentService;
		}
		
		
		
		/**
		 * 提交审核
		 * 
		 * @param mapping
		 * @param form
		 * @param request
		 * @param response
		 * @return ActionForward
		 * @throws Exception
		 */
		public ActionForward submitWorkFlowOutsourceContract(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response)
				throws Exception {
			log.debug("submitYfOutsourceContract");
			String submitType = request.getParameter("submitType");
			//新增界面直接提交
			if(StringUtils.isNotBlank(submitType) && "add".equals(submitType)){
				yfOutsourceContract = this.getYfOutsourceContract(form);
				 String outNo = yfOutsourceContractService.getMaxOutNo(yfOutsourceContract.getProjectNo());
				 yfOutsourceContract.setOutsourceNo(outNo);
				yfOutsourceContract.setOperateid(CommonUtil.getCurrentUser(request).getId());
				yfOutsourceContract.setCreateDate(new Date());
				this.yfOutsourceContractService.insertObject(yfOutsourceContract);
			}
			if(StringUtils.isNotBlank(submitType) && "update".equals(submitType)){
				yfOutsourceContract = this.getYfOutsourceContract(form);
				YfOutsourceContractForm tempForm = new YfOutsourceContractForm();
				tempForm.setId(yfOutsourceContract.getId());
				YfOutsourceContract old = yfOutsourceContractService.searchYfOutsourceContract(tempForm,null).get(0);
				yfOutsourceContract.setProjectType(old.getProjectType());
				yfOutsourceContract.setProjectOrg(old.getProjectName());
			}else{
				YfOutsourceContractForm yfOutsourceContractForm = (YfOutsourceContractForm) form;
				yfOutsourceContract = yfOutsourceContractService.searchYfOutsourceContract(yfOutsourceContractForm,null).get(0);
			}
			Map<String,String> mapVar = new HashMap<String,String>();
			mapVar.put("bussId", yfOutsourceContract.getId().toString());
			mapVar.put("bussType", WorkFlowConstants.BUSS_TYPE_WXHT.toString());
			mapVar.put("projectName", yfOutsourceContract.getProjectName().toString());
			mapVar.put("projectType", yfOutsourceContract.getProjectType().toString());
			mapVar.put("projectOrg", yfOutsourceContract.getProjectOrg().toString());
			mapVar.put("projectNo", yfOutsourceContract.getProjectNo().toString());
			mapVar.put("ownYear","0");  //外协合同 暂时不放  
			CommonUtil.putMapVar(request,mapVar);
			String processId = workFlowCommon.startFlow(WorkFlowConstants.PORCESS_DEFINITION_ONE,CommonUtil.getCurrentUser(request).getId(),mapVar);
			yfOutsourceContract.setProcessId(processId);
			yfOutsourceContract.setAuditStatus(WorkFlowConstants.AUDIT_STATUS_2);
			yfOutsourceContract.setOperateid(CommonUtil.getCurrentUser(request).getId());
			yfOutsourceContract.setCreateDate(new Date());
			this.yfOutsourceContractService.updateObject(yfOutsourceContract);
			return mapping.findForward(Constants.SUCCESS);
		}

		/**
		 * 进入审核页面
		 * @param mapping
		 * @param form
		 * @param request
		 * @param response
		 * @return ActionForward
		 * @throws Exception
		 */
		public ActionForward toAuditYfOutsourceContract(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
			log.debug("toAuditYfOutsourceContract");
			YfOutsourceContractForm yfOutsourceContractForm = (YfOutsourceContractForm) form;
			String bussId = request.getParameter("bussId");
			String processId = request.getParameter("processId");
			String workItemId = request.getParameter("workItemId");
			String bussType = request.getParameter("bussType");
			String auditRole = request.getParameter("auditRole");
			yfOutsourceContractForm.setId(bussId);
			yfOutsourceContract = this.yfOutsourceContractService.searchYfOutsourceContract(yfOutsourceContractForm, null).get(0);
			yfOutsourceContract.setWorkItemId(workItemId);
			request.setAttribute(Constants.YFOUTSOURCECONTRACT, yfOutsourceContract);
			request.setAttribute("bussType",bussType);
			request.setAttribute("auditRole", auditRole);
			examineList(processId,yfExmaineService,request);
			request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
			return mapping.findForward(Constants.SUCCESS);
		}
		
		
		/**
		 * 处理审批 提交流程
		 * @param mapping
		 * @param form
		 * @param request
		 * @param response
		 * @return
		 * @throws Exception
		 */
		public ActionForward commandFlowYfOutsourceContract(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
			log.debug("commandFlowYfOutsourceContract");
			YfOutsourceContractForm yfOutsourceContractForm = (YfOutsourceContractForm) form;
			yfOutsourceContract = this.yfOutsourceContractService.searchYfOutsourceContract(yfOutsourceContractForm, null).get(0);
			YfExmaine examine = addYfExmaine(yfOutsourceContractForm,request);
			Map processVar = new HashMap();
			//不为打回修改完成后提交
			if(!examine.getExamineFlag().equals("modify")){
				yfExmaineService.insertObject(examine);
				if(examine.getExamineFlag().equals(WorkFlowConstants.AUDIT_STATUS_AGREE)){
					processVar.put("approveFlag","agree");
					Usertable user = (Usertable) request.getSession().getAttribute("loginUser");
					String roleName = user.getRoleList().get(0).getName();
					if(roleName.equals(WorkFlowConstants.ROLE_FGFZ)){
						yfOutsourceContract.setAuditStatus(WorkFlowConstants.AUDIT_STATUS_3);
						yfOutsourceContractService.updateObject(yfOutsourceContract);
					}
				}else if(examine.getExamineFlag().equals(WorkFlowConstants.AUDIT_STATUS_DISAGREE)){
					processVar.put("approveFlag","disagree");
					yfOutsourceContract.setAuditStatus(WorkFlowConstants.AUDIT_STATUS_4);
					yfOutsourceContractService.updateObject(yfOutsourceContract);
				}
			}
			
			
			processVar.put("bussId", yfOutsourceContract.getId().toString());
			processVar.put("bussType", WorkFlowConstants.BUSS_TYPE_WXHT.toString());
			processVar.put("projectName", yfOutsourceContract.getProjectName().toString());
			processVar.put("projectType", yfOutsourceContract.getProjectType().toString());
			processVar.put("projectOrg", yfOutsourceContract.getProjectOrg().toString());
			processVar.put("projectNo", yfOutsourceContract.getProjectNo().toString());
			processVar.put("ownYear", "0");
			processVar.put("yfbussTaskType",WorkFlowConstants.BUSS_TASK_TYPE_NORMAL);
			/**
			 * 打回修改后需要提交  根据角色决定流程路径
			 */
			CommonUtil.putMapVar(request,processVar);
			processVar.put("yfbussTaskType",WorkFlowConstants.BUSS_TASK_TYPE_NORMAL);
			String workItemId = yfOutsourceContractForm.getWorkItemId();
			if(examine.getExamineFlag().equals(WorkFlowConstants.AUDIT_STATUS_REBACK)){
				processVar.put("yfbussTaskType",WorkFlowConstants.BUSS_TASK_TYPE_REBACK);
				//如果记录operatorid 人  的角色为研发经理的时候，回退就需要跳过研发经理审批  流程开始的参数须为 START_YFJL
//				workFlowCommon.commandFlowReback(workItemId, processVar,START_YFJL);
				String operateId = yfOutsourceContract.getOperateid();
				if(CommonUtil.isYFJL(operateId)){
					workFlowCommon.commandFlowReback(workItemId, processVar,YfWorkFlowDefine.START_YFJL);
				}else{
				    workFlowCommon.commandFlowReback(workItemId, processVar);
				}
			}else{
				workFlowCommon.commandFlow(workItemId, processVar);
			}
			return mapping.findForward(Constants.SUCCESS);
		}
		
		/**
		 * Description: TODO 组合审批意见对象,添加
		 * 
		 * @param form
		 * @param request
		 * @return ActionForward
		 */
		public YfExmaine addYfExmaine(ActionForm form, HttpServletRequest request) {

			YfOutsourceContractForm yfOutsourceContractForm = (YfOutsourceContractForm) form;
			// 审核标识
			String examineFlag = yfOutsourceContractForm.getExamineFlag();
			// 审核意见
			String examineOption = yfOutsourceContractForm.getExamineOption();
			String processId = yfOutsourceContractForm.getProcessId();
			YfExmaine yfExmaine = new YfExmaine();
			 
			yfExmaine.setId(RandomKey.generateID_Long_19());
			yfExmaine.setExamineFlag(examineFlag);
			yfExmaine.setExamineOption(examineOption);
			yfExmaine.setProcessId(processId);
			yfExmaine.setExamineDate(new Date());
			if(request.getSession().getAttribute("loginUser")!=null){
				Usertable curUser = (Usertable) request.getSession().getAttribute("loginUser");
				yfExmaine.setExaminePerson(curUser.getRname());
				if(curUser.getRoleList().size()>1){
					String roleName = workFlowCommon.getRoleNameByActivityId(yfOutsourceContractForm.getWorkItemId());
					yfExmaine.setExamineOrg(roleName);
				}else{
					yfExmaine.setExamineOrg(curUser.getRoleList().get(0).getName()); //无法处理同一个人有多个审批角色
				}
				
			}
			return yfExmaine;
		}
		
		 /*
		 * Description: TODO 查看显示意见
		 * 
		 * @param processInsId
		 * @param examine
		 * @param examineService
		 * @param examineLis
		 * @param request
		 *        void
		 */
		public void examineList(String processInsId, YfExmaineService yfExmaineService, HttpServletRequest request) throws Exception {
			YfExmaineForm yfExmaineForm = new YfExmaineForm();
			yfExmaineForm.setProcessId(processInsId);
			// 审批列表
			List<YfExmaine> examineList = yfExmaineService.searchYfExmaine(yfExmaineForm, null);
			// 防止页面出错
			int resultSize = 0;
			if(examineList != null && !examineList.isEmpty()) {
				resultSize = examineList.size();
			}
			DisplayTagPageBean.getPageBean("examine", resultSize, 0, request);
			request.setAttribute("examineList", examineList);

		}
		
		/**
		 * 到达更新页面
		 * 
		 * @param mapping
		 * @param form
		 * @param request
		 * @param response
		 * @return ActionForward
		 * @throws Exception
		 */
		public ActionForward toUpdateIncOutsourceContract(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
			log.debug("toUpdateIncOutsourceContract");
			String id = request.getParameter("id") == null ? "" : request.getParameter("id").toString();
			YfOutsourceContractForm yfOutsourceContractForm = (YfOutsourceContractForm) form;
			yfOutsourceContractForm.setId(id);
			yfOutsourceContract = this.yfOutsourceContractService.searchYfOutsourceContract(yfOutsourceContractForm, null).get(0);
			request.setAttribute(Constants.YFOUTSOURCECONTRACT, yfOutsourceContract);
			request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
			return mapping.findForward(Constants.SUCCESS);
		}
		
		/**
		 * 更新
		 * 
		 * @param mapping
		 * @param form
		 * @param request
		 * @param response
		 * @return ActionForward
		 * @throws Exception
		 */
		public ActionForward updateIncOutsourceContract(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
			log.debug("updateIncOutsourceContract");
			String id = request.getParameter("id") == null ? "" : request.getParameter("id").toString();
			YfOutsourceContract yfOutsourceContract = this.getYfOutsourceContract(form);
			yfOutsourceContract.setOperateid(CommonUtil.getCurrentUser(request).getId());
			yfOutsourceContract.setCreateDate(new Date());
			this.yfOutsourceContractService.updateObject(yfOutsourceContract);
			PrintWriter out = response.getWriter();
			out.print("<script>window.opener.document.location.reload(); self.close();</script>");
			return null;
		}
		
		
		public WorkFlowCommon getWorkFlowCommon() {
			return workFlowCommon;
		}

		public void setWorkFlowCommon(WorkFlowCommon workFlowCommon) {
			this.workFlowCommon = workFlowCommon;
		}

		public YfExmaineService getYfExmaineService() {
			return yfExmaineService;
		}

		public void setYfExmaineService(YfExmaineService yfExmaineService) {
			this.yfExmaineService = yfExmaineService;
		}	

}
